//
//  AppDelegate.swift
//  BlueToothBackground
//
//  Created by macvivi on 2021/11/3.
//

import UIKit
import CoreLocation

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    
    
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        let centralManagerIdenntifiers = launchOptions?[UIApplication.LaunchOptionsKey.bluetoothCentrals]
       
        if let arr = centralManagerIdenntifiers  {
            let arrStr:[String] = arr as! [String]
            MyFileManager.shared.sendBluetoothState(state: "didFinishLaunchingWithOptions", content: arrStr[0])
            XYCentralManager.shared.startScan { (periphral) in }
//            XYCentralManager.shared
        } else if let launch = launchOptions {
            let num = launch[UIApplication.LaunchOptionsKey.location] as! NSNumber
            MyFileManager.shared.sendBluetoothState(state: "didFinishLaunchingWithOptions", content: String(num.intValue))
        }else{
            MyFileManager.shared.sendBluetoothState(state: "didFinishLaunchingWithOptions", content: "noLaunchOptions")
        }
        
        //把这句代码注释掉就是只用蓝牙，不用ibeacon
        beacon()
       
        return true
    }
    
    
    let defaultUUID = "E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"
    var locationManager = CLLocationManager()
    func beacon(){
        locationManager.delegate = self
        self.locationManager.requestAlwaysAuthorization()
        let beaconRegion = CLBeaconRegion(proximityUUID: UUID(uuidString: defaultUUID)!, identifier: defaultUUID)
        beaconRegion.notifyEntryStateOnDisplay = true
        beaconRegion.notifyOnEntry = true
        beaconRegion.notifyOnExit = true
        self.locationManager.startMonitoring(for: beaconRegion)
//        self.locationManager.startRangingBeacons(in: beaconRegion)
    }


}

extension AppDelegate:CLLocationManagerDelegate{
    // MARK: - Location Manager Delegate
    /// - Tag: didDetermineState
    func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
        print(#function)
        print(state.rawValue)
        let beaconRegion = (region as? CLBeaconRegion)!
        if state == .inside {
            // Start ranging when inside a region.
//            manager.startRangingBeacons(satisfying: beaconRegion!.beaconIdentityConstraint)
//            manager.startMonitoring(for: region)
            manager.startRangingBeacons(in: beaconRegion)
        } else {
            // Stop ranging when not inside a region.
//            manager.stopRangingBeacons(satisfying: beaconRegion!.beaconIdentityConstraint)
//            manager.stopMonitoring(for: region)
            manager.stopRangingBeacons(in: beaconRegion)
        }
    }
    
    func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
        print(#function)
    }
    
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print(#function)
        MyFileManager.shared.sendBluetoothState(state: "didEnterRegion", content: "")
        XYCentralManager.shared.startScan { (periphral) in }
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        MyFileManager.shared.sendBluetoothState(state: "didExitRegion", content: "")
        print(#function)
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        print(#function)
    }
    
    func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
        printXY(#function, obj: self, line: #line)
        MyFileManager.shared.sendBluetoothState(state: "didRangeBeacons", content: "")
        if beacons.count > 0 {
            let beacon = beacons[0]
            printXY(beacon.proximity.rawValue, obj: self, line: #line)
        }

    }
    
//    func locationManager(_ manager: CLLocationManager, didRange beacons: [CLBeacon], satisfying beaconConstraint: CLBeaconIdentityConstraint) {
//        let beacon = beacons[0]
//        printXY(beacon.proximity.rawValue, obj: self, line: #line)
//    }
    
}

